// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.properties.cmd;

import org.dengues.core.components.NodePropertyParameter;
import org.dengues.designer.ui.database.parts.AbstractDBGraphicalEditPart;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.models.CompNode;
import org.dengues.model.database.AbstractDBObject;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * @since 2007-10-12 下午09:00:21
 */
public class PropertyChangeTextCommand extends AbstractPropertyCommand {

    private final Object newValue;

    private final String propName;

    private Object oldValue;

    private NodePropertyParameter elementParameter;

    private AbstractDBObject child;

    private AbstractDBGraphicalEditPart editPart;

    /**
     * Qiang.Zhang.Adolf@gmail.com PropertyChangeCommand constructor comment.
     * 
     * @param elem
     * @param name
     * @param text
     */
    public PropertyChangeTextCommand(Object elem, String name, Object text) {
        if (elem instanceof CompNode) {
            this.elem = (CompNode) elem;
            setLabel(Messages.getString("PropertyChangeTextCommand.change", this.elem.getCompName(), name)); //$NON-NLS-1$
        } else if (elem instanceof AbstractDBGraphicalEditPart) {
            editPart = (AbstractDBGraphicalEditPart) elem;
            child = ((AbstractDBGraphicalEditPart) elem).getCastModel();
            setLabel(Messages.getString("PropertyChangeTextCommand.change", child.getName(), name)); //$NON-NLS-1$
        }
        this.newValue = text;
        this.propName = name;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.cmd.AbstractPropertyCommand#execute()
     */
    @Override
    public void execute() {
        if (elem != null) {
            elementParameter = elem.getElementParameter(propName);
            oldValue = elementParameter.getValue();
            elementParameter.setValue(newValue);
        } else if (child != null) {
            elementParameter = editPart.getElementParameter(propName);
            elementParameter.setValue(newValue);
            oldValue = elementParameter.getValue();
            child.getProperties().put(propName, newValue);
        }
        refreshPropertyView();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.properties.cmd.AbstractPropertyCommand#undo()
     */
    @Override
    public void undo() {
        if (elem != null) {
            elementParameter.setValue(oldValue);
        } else if (child != null) {
            elementParameter.setValue(oldValue);
            child.getProperties().put(propName, oldValue);
        }
        refreshPropertyView();
    }

}
